import React from 'react';
import { loc, obj_str, is_empty } from 'sui';
import { load_plc, plc_value } from '../cmd/load';

const top_lever = (cmd) => {
    if (cmd.children) {
        return {...cmd, exp: false};
    }

    return {...cmd, lv: 20, exp: false};
};

const second_lever = (cmd) => {
    return {...cmd, lv: 50, exp: false};
};

const third_lever = (cmd) => {
    return {...cmd, lv: 80, exp: false};
};

const four_lever = (cmd) => {
    return {...cmd, lv: 110, exp: false};
};

const tree_col = (d, hcmd) => {
    let lv = d.lv;
    if (!lv) {
        lv = 0;
    }

    if (!hcmd) {
        return <div style={{marginLeft: lv + "px"}}>{d["m"]}</div>;
    }

    if (!d.children) {
        return <div style={{marginLeft: lv + "px"}}>{d["m"]}</div>;
    }

    return (
        <div className="inline-flex">
            <span style={{width: '20px', marginLeft: lv + "px"}} onClick={() => hcmd(d.id)}>
                {d.exp ? '\u25BC' : '\u25B6'}
            </span>
            {d["m"]}
        </div>
    );
};

const build_plc = (plc_main, cmd, setMain) => {
    const type = obj_str(cmd, "type");

    if (type === "plc") {
        load_plc(null, v => setMain(v));
    }
    else if (type === "update") {
        const val = cmd.v;
        const newRows = [];

        plc_main.forEach(d => {
            if (d.uid === val.uid) {
                d["id"] = val.id;
                d["value"] = val.name;
            }

            newRows.push(d);
        });

        newRows.sort((a, b) => a.value > b.value);

        setMain(newRows);
    }
    else if (type === "add") {
        const val = plc_value(cmd.v);
        if (!val) {
            return;
        }

        let newRows = [];

        if (!is_empty(plc_main)) {
            newRows = plc_main;
        }

        newRows.push(val);

        newRows.sort((a, b) => a.value > b.value);
        setMain(newRows);
    }
    else if (type === "delplc") {
        const id = obj_str(cmd, "id");

        const newRows = [];

        plc_main.forEach(d => {
            if (d.id === id) {
                return;
            }

            newRows.push(d);
        });

        setMain(newRows);
    }
    else if (type === "it") {
        const id = obj_str(cmd, "id");
        const data = cmd.v;

        const newRows = [];

        plc_main.forEach(d => {
            if (d.id === id) {
                let tmp = {};
                tmp = d;

                if (is_empty(data)) {
                    tmp["sub"] = null;
                }
                else {
                    tmp["sub"] = data;
                }

                newRows.push(tmp);

                return;
            }

            newRows.push(d);
        });

        setMain(newRows);
    }
};

const build_sub = (plc_main, id, setRow) => {
    const row = plc_main.find(d => d.id === id);

    if (!row.children) {
        return;
    }

    const set = new Set();
    const newRows = [];

    plc_main.forEach(d => {
        if (set.has(d.id)) {
            return;
        }

        if (d.id !== id) {
            newRows.push(d);
            return;
        }

        let tmp = {};
        tmp = d;

        tmp.exp = !row.exp;

        newRows.push(tmp);

        if (tmp.exp) {
            tmp.children.forEach(t => newRows.push(t));
            return;
        }

        tmp.children.forEach(t => {
            set.add(t.id);

            if (t.children) {
                t.children.forEach(tt => {
                    set.add(tt.id);

                    if (tt.children) {
                        tt.children.forEach(ttt => set.add(ttt.id));
                    }
                });
            }
        })
    });

    setRow(newRows);
};

const expand_sub = (main, plc_main, setRow) => {
    const newRows = [];

    const m = obj_str(main, "m");
    const uid = obj_str(main, "uid");
    const tp = obj_str(main, "tp");

    if (tp !== "call") {
        newRows.push({
            m: uid,
            k: "",
            lv: 20,
            ltp: loc("cmd_15"),
        });
    }

    if (m) {
        newRows.push({
            m: m,
            k: obj_str(main, "trigger"),
            lv: 20,
            ltp: loc("mg_269"),
        });
    }

    plc_main.forEach(d => {
        newRows.push({
            m: d.m,
            k: d.k,
            lv: 20,
            ltp: d.ltp,
            children: d.children ? 1 : 0,
        });

        if (d.children) {
            d.children.forEach(t => {
                newRows.push({
                    m: t.m,
                    k: t.k,
                    lv: 40,
                    ltp: t.ltp,
                    children: t.children ? 1 : 0,
                });

                if (t.children) {
                    t.children.forEach(tt => {
                        newRows.push({
                            m: tt.m,
                            k: tt.k,
                            lv: 60,
                            ltp: tt.ltp,
                            children: tt.children ? 1 : 0,
                        });

                        if (tt.children) {
                            tt.children.forEach(ttt => {
                                newRows.push({
                                    m: ttt.m,
                                    k: ttt.k,
                                    lv: 80,
                                    ltp: ttt.ltp,
                                    children: ttt.children ? 1 : 0,
                                });
                            });
                        }
                    });
                }
            });
        }
    });

    setRow(newRows);
};

export {build_sub, expand_sub, build_plc, four_lever, top_lever, second_lever, third_lever, tree_col};
